package com.example.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.stu.entity.PageMessage;
import com.example.stu.entity.PageResult;
import com.example.stu.entity.Result;
import com.example.stu.entity.resp.StuMessageInfo;
import com.example.stu.mapper.StuMessageMapper;
import com.example.stu.mapper.StudentMapper;
import com.example.stu.pojo.StuMessage;
import com.example.stu.pojo.Student;
import com.example.stu.service.StuMessageService;
import com.mysql.jdbc.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2021/3/22
 * @Author: 黄先军
 * @Description:
 */
@Service
public class StuMessageServiceImpl implements StuMessageService {

    @Resource
    private StuMessageMapper stuMessageMapper;

    @Resource
    private StudentMapper studentMapper;

    /**
     * 学生提交留言
     * @param stuMessage
     * @return
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result messageTiJiao(StuMessage stuMessage) {
        if (StringUtils.isNullOrEmpty(stuMessage.getInfomessage())) {
            return new Result(false,"留言不能为空");
        }
        //通过传来的学生学号去寻找对应的学生姓名
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId,stuMessage.getStuId()));
        stuMessage.setStuName(student.getStuName());
        stuMessage.setCreateTime(new Date());
        stuMessageMapper.insert(stuMessage);
        return new Result(true,"提交留言成功");

    }

    /**
     * 查看问题详情
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        StuMessage stuMessage = stuMessageMapper.selectOne(new LambdaQueryWrapper<StuMessage>().eq(StuMessage::getId, id));
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId, stuMessage.getStuId()));
        if(student==null){
            return new Result(false,"查询留言失败");
        }
        StuMessageInfo info = new StuMessageInfo();
        info.setStuId(student.getStuId());
        info.setStuName(stuMessage.getStuName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(stuMessage.getCreateTime());
        info.setThisTime(format);
        info.setPhoneNumber(student.getStuPhone());
        info.setThisEmail(student.getStuMail());
        info.setThisType(stuMessage.getThistype());
        info.setThisInfo(stuMessage.getInfomessage());
        return new Result(true,"查询信息成功",info);
    }

    /**
     * 带条件的分页查询
     * @param pageMessage
     * @return
     */
    @Override
    public PageResult findPage(PageMessage pageMessage) {
        Page<StuMessage> queryPage = new Page<>(pageMessage.getCurrentPage(),pageMessage.getPageSize());
        //查询击中的行数
        List<StuMessage> list = stuMessageMapper.pages(pageMessage);
        //分页后的数据
        List<StuMessage> pages = stuMessageMapper.pages(queryPage, pageMessage);
        return new PageResult((long)list.size(), pages);
    }

    /**
     * 解决问题
     * @param id
     * @return
     */
    @Override
    public Result remove(Integer id) {
        try {
            stuMessageMapper.deleteById(id);
        }catch (Exception exception){
            return new Result(false,"删除该条信息失败");
        }
        return new Result(true,"已经解决该问题");
    }

}
